package com.czl.module_storehouse.adapter;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.czl.base.data.bean.tengyun.ProductBean;
import com.czl.base.data.bean.tengyun.SortBean;
import com.czl.base.data.bean.tengyun.SortBeanType;
import com.czl.module_storehouse.R;
import com.czl.module_storehouse.listener.OnItemChangeListener;

import java.util.List;

/**
 * @author xubin
 */
public class BorrowAddAdapter extends BaseMultiItemQuickAdapter<SortBean, BaseViewHolder> {
    private final OnItemChangeListener mOnItemChangeListener;

    public BorrowAddAdapter(@Nullable List<SortBean> data, OnItemChangeListener onItemChangeListener) {
        super(data);
        mOnItemChangeListener = onItemChangeListener;
        addItemType(SortBeanType.Type.VIEW_TYPE_SORT, R.layout.item_borrow_add);
        addItemType(SortBeanType.Type.VIEW_TYPE_PRODUCT, R.layout.item_receive_product);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder helper, SortBean item) {
        switch (item.getItemType()) {
            case SortBeanType.Type.VIEW_TYPE_SORT:
                helper.setText(R.id.tv_title, item.getSortName())
                        .setText(R.id.tv_model, "型号：" + item.getSortModel())
                        .setText(R.id.tv_num, String.valueOf(item.getReceiveCount()));
                helper.getView(R.id.text_remove).setOnClickListener(v -> {
                    remove(item);
                    if (mOnItemChangeListener != null) {
                        mOnItemChangeListener.onChange(helper.getAbsoluteAdapterPosition() - getHeaderLayoutCount());
                    }
                });
                helper.getView(R.id.cl_root)
                        .setOnClickListener(v -> {
                            if (getOnItemChildClickListener() != null) {
                                getOnItemChildClickListener().onItemChildClick(BorrowAddAdapter.this, v, helper.getAbsoluteAdapterPosition() - getHeaderLayoutCount());
                            }
                        });
                helper.getView(R.id.text_remove)
                        .setOnClickListener(v -> remove(item));
                break;
            case SortBeanType.Type.VIEW_TYPE_PRODUCT:
                ProductBean productBean = item.getProductBean();
                if (productBean == null) {
                    return;
                }
                helper.setText(R.id.tv_title, productBean.getSortName())
                        .setText(R.id.tv_model, "型号：" + (productBean.getProductModel() == null ? "" : productBean.getProductModel()))
                        .setText(R.id.tv_code, "编号：" + (productBean.getProductCode() == null ? "" : productBean.getProductCode()))
                        .setText(R.id.tv_loc, "位置：" + (productBean.getStorehouseLocatName() == null ? "" : productBean.getStorehouseLocatName()));
                helper.getView(R.id.tv_remove)
                        .setOnClickListener(v -> {
                            remove(item);
                            if (mOnItemChangeListener != null) {
                                mOnItemChangeListener.remove(helper.getLayoutPosition());
                            }
                        });
                break;
            default:
                break;

        }
    }

    @Override
    public void addData(@NonNull SortBean data) {
        super.addData(data);
        if (mOnItemChangeListener != null) {
            mOnItemChangeListener.add(getData().size() + getHeaderLayoutCount());
        }
    }
}

